/**
 * 设置缓存 
 * @param key 
 * @param value 
 */
export function setStorage(key, value){
  return new Promise(resolve => {
    wx.setStorage({key, data: value}).then(() => {
      resolve(true)
    }, () => resolve(false))
  })
}


export function getStorageInfo() {
  return new Promise(resolve => {
    wx.getStorageInfo().then((res) => resolve(res), () => resolve(null))
  })
}

/**
 * 获取指定缓存
 * @param key 
 */
export function getStorage(key) {
  return new Promise(resolve => {
    wx.getStorage({ key }).then((res) => resolve(res.data), () => resolve(null))
  })
}

/**
 * 删除指定缓存
 * @param key 
 */
export function removeStorage(key){
  return new Promise(resolve => {
    wx.removeStorage({ key }).then(() => resolve(true), () => resolve(false))
  })
}

/**
 * 清空所有缓存
 */
export function clearStorage() {
  wx.clearStorage();
}

/**
 * [时间格式转换]
 * @param  {[type]} date    [description]
 * @param  {[type]} formats [description]
 * @return {[type]}         [description]
 */
export function formatTime(date, formats){
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  formats = formats || 'Y-m-d';
  return formats.replace(/Y|m|d|H|i|s/ig, (matches) => {
    return ({
      Y: year,
      m: month,
      d: day,
      H: hour,
      i: minute,
      s: second
    })[matches];
  })

  // return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

export function formatNumber(n){
  n = n.toString()
  return n[1] ? n : '0' + n
}